[% setvar title TITLE %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>Localising Paren Counts in qr()s.</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Richard Proctor &lt;<a href='mailto:richard@waveney.org'>richard@waveney.org</a>&gt;
  Date: 24 Sep 2000
  Last Modified: 30 Sep 2000
  Mailing List: <a href='mailto:perl6-language-regex@perl.org'>perl6-language-regex@perl.org</a>
  Number: 276
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p>The Paren Counts and backreferences should be localised in each qr(), to prevent
surprises when qr()s are used in combination.</p>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>TomCs perl storm #0040 has:</p>
<p>&gt; Figure out way to do
&gt;
&gt;     /$e1 $e2/
&gt;
&gt; safely, where $e1 might have '(foo) \1' in it.
&gt; and $e2 might have '(bar) \1' in it.  Those won't work.</p>
<a name='DISCUSSION'></a><h2>DISCUSSION</h2>
<p>Me: If e1 and e2 are qr// type things the answer might be to localise
the backref numbers in each qr// expression.   Use of assignment in a regex
and named backrefs (RFC 112) would make this a lot safer.</p>
<p>Hugo:
I think it is reaonable to ask whether the current handling of qr{}
subpatterns is correct:</p>
<p>perl -wle '$a=qr/(a)\1/; $b=qr/(b).*\1/; /$a($b)/g and print join &quot;:&quot;, $1,
pos for &quot;aabbac&quot;'
a:5</p>
<p>I'm tempted to suggest it isn't; that the paren count should be local
to each qr{}, so that the above prints 'bb:4'. I think that most people
currently construct their qr{} patterns as if they are going to be
handled in isolation, without regard to the context in which they are
embedded - why else do they override the embedder's flags if not to
achieve that?</p>
<p>The problem then becomes: do we provide a mechansim to access the
nested backreferences outside of the qr{} in which they were referenced,
and if so what syntax do we offer to achieve that? I don't have an answer
to the latter, which tempts me to answer 'no' to the former for all the
wrong reasons. I suspect (and suggest) that complication is the only
reason we don't currently have the behaviour I suggest the rest of the
semantics warrant - that backreferences are localised within a qr().</p>
<p>I lie: the other reason qr{} currently doesn't behave like that is that
when we interpolate a compiled regexp into a context that requires it be
recompiled, we currently ignore the compiled form and act only on the
original string. Perhaps this is also an insufficiently intelligent thing
to do.</p>
<p>MJD:
Interpolated qr() items shouldn't be recompiled anyway.  They should
be treated as subroutine calls.  Unfortunately, this requires a
reentrant regex engine, which Perl doesn't have.  But I think it's the
right way to go, and it would solve the backreference problem, as well
as many other related problems.</p>
<p>Me: You can access the nested backreferences outside of the qr{} in which
they were referenced by use of the named backref see RFC 112.</p>
<a name='AGREEMENTS'></a><h2>AGREEMENTS</h2>
<p>The paren count in each qr() is localised to each qr().</p>
<p>There is no way to access the nested backrefernces outside of the qr() by
number they may be accessed by name (see RFC 112).</p>
<p>The regex engine must be made re-entrant.</p>
<p>The regex compiler should not need to recompile qr()s when used as part of
another regex.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<p>V2 - Added a some more comments under implementation and froze -
no significant changes.</p>
<a name='IMPLENTATION'></a><h1>IMPLENTATION</h1>
<p>The Regex engine must be made re-entrant.</p>
<p>The expansion of variables in regexes must be driven by the regex compiler
(Same problem as for RFCs 112, 166 ...)</p>
<p>Hugo:</p>
<p>None of these are necessarily true - we could change the overloading
of the Regexp object instead. Currently we have:</p>
<pre>  my $re = qr{pattern};
  print &quot;$re&quot;;</pre>
<p>.. giving 'pattern' by overloading stringification. If we overload it
instead to give '(??{ $re })' (or a moral equivalent) we have a nasty
hack, it is true, but it could allow us to defer the much trickier
proper solution. Of course it breaks every other use of the string
value, and I'm not sure how big a problem that might be.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>Perlstorm #0040 from TomC.</p>
<p>RFC 112: Assignment within a regex</p>
</div>
